package ps.client.gui.util.textpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Vector;

import javax.swing.JLabel;


@SuppressWarnings("serial")
public class TextLine extends Container {
	
	int widthOffset = 0;
	int heightOffset = 0;
	
	int width = 0;
	int height = 0;
	
	int minWidth = 200;
	int maxWidth = 600;
//	int hGap = 2;
	
	boolean lineBreak = false;

	Vector<Component> fullCompList = new Vector<Component>();
	Vector<LinePart> linePartList = new Vector<LinePart>();
	
	public TextLine() {
	}
	
	public TextLine(LinePart[] lineParts) {
		this();
		setLayout(null);
		for (int i = 0; i < lineParts.length; i++) {
			addLinePart(lineParts[i]);
		}
	}
	
	public TextLine(LinePart linePart) {
		this(new LinePart[] { linePart });
	}
	
	public void addLinePart(LinePart linePart) {
		linePartList.add(linePart);
		internalAddLinePart(linePart);
		setPreferredSize(new Dimension(width, height));
	}
	
	private void internalAddLinePart(LinePart linePart) {
//		if (linePart.comp instanceof JLabel) {
//			((JLabel)linePart.comp).setBorder(new LineBorder(Color.WHITE, 1));
//		}
		
		linePart.comp.setBounds(widthOffset + linePart.leftGap, heightOffset + linePart.vOffset, linePart.comp.getPreferredSize().width, linePart.comp.getPreferredSize().height);
		add(linePart.comp);
		
		widthOffset += linePart.comp.getPreferredSize().width + linePart.leftGap + linePart.rightGap;
		width = widthOffset > width ? widthOffset : width;
		int newHeight = linePart.comp.getPreferredSize().height + heightOffset + linePart.vOffset;
		if (newHeight > height) {
			height = newHeight;
		}
	}
	
	public void appendText(String str, Font font, Color fg, int leftGap, int rightGap, int vOffset) {
		JLabel label = new JLabel(str);
		label.setFont(font);
		label.setForeground(fg);
		LinePart linePart = new LinePart(label, leftGap, rightGap, vOffset);
		addLinePart(linePart);
	}
	
	public void setMaximumWidth(int maxWidth) {
		this.maxWidth = maxWidth > minWidth ? maxWidth : minWidth; 
		validate();
	}
	
	public void validate() {
		if (lineBreak || width > maxWidth) {
			doLayout();
		}
	}
	
	public void doLayout() {
		removeAll();
		widthOffset = 0;
		heightOffset = 0;
		width = 0;
		height = 0;
		lineBreak = false;
		
		for (int i = 0; i < linePartList.size(); i++) {
			LinePart linePart = linePartList.get(i);
			if (widthOffset + linePart.comp.getWidth() > maxWidth) {
				lineBreak = true;
				if (linePart.allowLineBreak) {
					if (linePart.comp instanceof JLabel) {
						JLabel label = (JLabel)linePart.comp;
						String str = label.getText();
						int i1 = 0;
						int i2 = 0;
						int i3 = 0;
						while (str.length() > i3) {
						    // i2 becomes word beginning index
							while (i2 < str.length() && str.charAt(i2) == ' ') {
								i2++;
							}
							// i2 becomes word ending index or str ending index
							i2 = str.indexOf(' ', i2);
							i2 = i2 < 0 ? str.length() : i2;
							JLabel splitter = new JLabel(str.substring(i1, i2));
							splitter.setFont(label.getFont());
							splitter.setForeground(label.getForeground());
							if (widthOffset + splitter.getPreferredSize().width < maxWidth) {
    							while (widthOffset + splitter.getPreferredSize().width < maxWidth && i2 < str.length()) {
    								i3 = i2;
    								while (i2 < str.length() && str.charAt(i2) == ' ') {
    									i2++;
    								}
    								i2 = str.indexOf(' ', i2);
    								i2 = i2 < 0 ? str.length() : i2;
    								splitter.setText(str.substring(i1, i2));
    							}
    							if (widthOffset + splitter.getPreferredSize().width > maxWidth) {
    								i2 = i3;
    							}
							} else { // else its one long word
							    while ((widthOffset + splitter.getPreferredSize().width > maxWidth) && (i2 - i1 > 2)) {
							        i2--;
							        splitter.setText(str.substring(i1, i2));
							    }
							}
							splitter.setText(str.substring(i1, i2));
							internalAddLinePart(new LinePart(splitter));
							i1 = i2;
							i3 = i2;
							if (i3 < str.length()) {
								widthOffset = 0;
								heightOffset = height;
							}
						}
					}
				} else {
					widthOffset = 2;
					heightOffset = i > 0 ? height : heightOffset;
					internalAddLinePart(linePart);
				}
			} else {
				internalAddLinePart(linePart);
			}
		}
		setPreferredSize(new Dimension(width, height));
	}

}
